package com.soulkun.smarthome.ui.main.home.dialog

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppDialogMainHomeAddDeviceNameBinding
import com.soulkun.smarthome.databinding.AppDialogMainPersonalRenameBinding
import com.soulkun.smarthome.logic.Constant
import com.soulkun.smarthome.ui.main.home.AppMainHomeAddDeviceActivity
import com.soulkun.smarthome.ui.main.home.AppMainHomeFragment
import com.soulkun.smarthome.ui.main.home.viewmodel.AppMainHomeAddDeviceViewModel
import com.soulkun.smarthome.ui.main.personal.viewmodel.AppMainPersonalFamilyCreateViewModel
import com.soulkun.smarthome.ui.main.personal.viewmodel.AppMainPersonalRenameViewModel
import com.soulkun.smarthome.ui.main.personal.viewmodel.AppMainPersonalViewModel
import soulkun.library.mvvm.component.BaseDataBindingDialogFragment
import soulkun.library.utils.getAppContext
import soulkun.library.utils.logE
import soulkun.library.utils.toPx
import soulkun.library.utils.toast
import java.lang.Exception

class AppMainHomeAddDeviceNameDialog(
    private val activity: Activity,
    private val viewModel: AppMainHomeAddDeviceViewModel,
    private val onFinishListener: () -> Unit
) : BaseDataBindingDialogFragment<AppDialogMainHomeAddDeviceNameBinding>() {

    override fun setLayout(): Int {
        return R.layout.app_dialog_main_home_add_device_name
    }

    override fun doOnViewCreated() {
        requireDialog().window?.run {
            setLayout(requireContext().resources.getDimensionPixelSize(R.dimen.sw_300dp), ViewGroup.LayoutParams.WRAP_CONTENT)
        }
        isCancelable = false
        requireDataBinding().lifecycleOwner = viewLifecycleOwner
        requireDataBinding().viewModel = viewModel
        requireDataBinding().onClickListener = View.OnClickListener {
            when (it.id) {
                R.id.iv_main_home_add_device_icon_delete -> {
                    viewModel.deviceNameEditLiveData.value = ""
                }
                R.id.tv_main_home_add_device_text_cancel -> {
                    dismissAllowingStateLoss()
                    onFinishListener()
                }
                R.id.tv_main_home_add_device_text_save -> {
                    try {
                        viewModel.addRoomDevice(viewModel.deviceNameEditLiveData.value!!)
                            .observe(viewLifecycleOwner) { result ->
                                result.getOrNull()?.let { response ->
                                    response.statusMessage.toast()
                                    activity.setResult(Constant.ACTIVITY_RESULT_ADD_DEVICE)
                                    dismissAllowingStateLoss()
                                }
                                onFinishListener()
                            }
                    } catch (e: Exception) {
                        e.printStackTrace()
                        "扫描结果错误，请重新扫描".toast()
                        onFinishListener()
                    }
                }
            }
        }
    }

    override fun doOnResume() {
        requireDataBinding().etMainHomeAddDeviceTextDeviceName.run {
            requestFocus()
        }
    }

    override fun doOnDismiss() {
    }

    override fun doOnDestroyView() {
    }

}